﻿@{
    Ajax.Dna().Widget()
        .UserPreferences(@<text>
    <p>@Html.Global("RES_SUBJECT")</p>
    <p>@Ajax.Dna().TextBox(item.GenerateFieldID("Subject")).Value(item.GetString("Subject")).GetHtml()</p>
    <p>
        @Html.Global("RES_RECEIVER")</p>
    <p>@Ajax.Dna().TextBox(item.GenerateFieldID("Receiver")).Value(item.GetString("Receiver")).GetHtml()</p>
    <p>@Html.Global("RES_RETURNTEXT")</p>
    <p>
        @Ajax.Dna().TextArea(item.GenerateFieldID("ResponseMessage")).Value(item.GetString("ResponseMessage")).GetHtml()
    </p>
    </text>)
        .Content(@<text>
    <form id="@item.GenerateFieldID("form")" method="post" action="javascript:void(0);" >
    @Html.AntiForgeryToken()
    @Html.ValidationSummary()
    @{
        var vals = new Dictionary<string, object>();
        vals.Add("data-val", "true");
        vals.Add("data-val-required", "Name required");

        var emailVals = new Dictionary<string, object>();
        emailVals.Add("data-val", "true");
        emailVals.Add("data-val-required", "Email required");

        var contentlVals = new Dictionary<string, object>();
        contentlVals.Add("data-val", "true");
        contentlVals.Add("data-val-required", "Body required");
        MembershipUser user = null;
        if (Request.IsAuthenticated)
        {
            user = Membership.GetUser(User.Identity.Name);
        }

        var to = item.GetString("Receiver");
        if (string.IsNullOrEmpty(to))
        {
            to = WebSiteContext.Current.Web.Owner;
        }
        var subject = item.GetString("Subject");

        if (string.IsNullOrEmpty(subject))
        {
            subject = "Contact";
        }
    }
    <table>
        <tr>
            <td>
                @Html.Global("RES_NAME") :
            </td>
        </tr>
        <tr>
            <td>
                @if (!Request.IsAuthenticated)
                {
                    @Ajax.Dna().TextBox(item.GenerateFieldID("Name")).HtmlAttributes(vals).GetHtml()
                }
                else
                {
                    @user.GetDisplayName() 
                    <input type="hidden" value="@user.UserName" id="@item.GenerateFieldID("Name")" />
                }
            </td>
        </tr>
        <tr>
            <td>
                @Html.Global("RES_EMAIL"):
            </td>
        </tr>
        <tr>
            <td>
                @if (!Request.IsAuthenticated)
                {
                    @Ajax.Dna().TextBox(item.GenerateFieldID("From")).HtmlAttributes(emailVals).GetHtml()
                }
                else
                {
                    @user.Email
                    <input type="hidden" value="@user.Email" id="@item.GenerateFieldID("From")" />
                }
            </td>
        </tr>
        <tr>
            <td>
                @Html.Global("RES_CONTENT")
            </td>
        </tr>
        <tr>
            <td>
                @Ajax.Dna().TextArea(item.GenerateFieldID("Body")).HtmlAttributes(contentlVals).Height(250).Resizable().GetHtml()
            </td>
        </tr>
        <tr>
            <td colspan="2" style="text-align: center">
                @{
                    var scripts = new System.Text.StringBuilder();
                    scripts.Append("$(\"#" + item.GenerateFieldID("form") + "\")")
                              .AppendLine(".submit(function(event){ ")
                              .AppendLine("if (!event.isDefaultPrevented()) {")
                              .AppendLine("$.ajax({")
                              .AppendLine("type:\"POST\",")
                              .AppendLine("url:\"" + Url.Action("MailTo", "SocialNetwork", new { Area = "" }) + "\",")
                              .AppendLine("success:function(){")
                              .AppendLine("$(\"#" + item.GenerateFieldID("form") + "\").remove();")
                              .AppendLine("$(\"#" + item.GenerateFieldID("_result") + "\").toggle();")
                              .AppendLine("},")
                              .AppendLine("data:{")
                              .AppendLine("from:$(\"#" + item.GenerateFieldID("From") + "\").val(),")
                              .AppendLine("to:\"" + to + "\",")
                              .AppendLine("subject:\"" + subject + "\",")
                              .AppendLine("body:$dna.htmlEncode($(\"#" + item.GenerateFieldID("Body") + "\").val()),")
                              .AppendLine("__RequestVerificationToken:$(this).find(\"input[name='__RequestVerificationToken']\").val()")
                              .AppendLine("}")
                              .AppendLine("});}});");
                    Html.RegisterStartupScript(scripts.ToString());
                }
                @Ajax.Dna().Button(item.GenerateFieldID("btnSend")).ImageIcons(Url.Content("~/content/images/icon_mail_16.png")).Submit().LocText("RES_SEND").GetHtml()
            </td>
        </tr>
    </table>
    </form>
    <div id="@item.GenerateFieldID("_result")" style="padding:10px;display:none;">
        @if (!string.IsNullOrEmpty(item.GetString("ResponseMessage")))
        {
            @MvcHtmlString.Create(item.GetString("ResponseMessage").Replace("\r\n", "<br/>"))
        }
        else
        {
            <p>
                Thank you!</p>
            <p>
                You message have been send.</p>
        }
    </div>
    </text>)
.Render();
}
